package com.dlc.apk.utils

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import android.view.inputmethod.InputMethodManager
import com.dlc.apk.base.BaseApp.Companion.instance
import java.io.File

object DeviceUtil {
    val packageName: String?
        get() {
            val packageManager = instance.packageManager
            try {
                val packageInfo = packageManager.getPackageInfo(instance.packageName, 0)
                return packageInfo.packageName
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }
    val packageVersion: String?
        get() {
            val packageManager = instance.packageManager
            try {
                val packageInfo = packageManager.getPackageInfo(instance.packageName, 0)
                return packageInfo.versionName
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }
    val packageVersionCode: Int
        get() {
            val packageManager = instance.packageManager
            try {
                val packageInfo = packageManager.getPackageInfo(instance.packageName, 0)
                return packageInfo.versionCode
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return 0
        }

    //测试阶段
    private const val debug = false //BuildConfig.DEBUG;

    /**
     * 获取设备的 deviceId
     */
    val androidId: String
        get() {
            if (debug) {
                return "cb378ff9dc5a3e84"
            }
            val androidId = Settings.System.getString(instance.contentResolver, Settings.Secure.ANDROID_ID)
            return if (TextUtils.isEmpty(androidId)) {
                Build.SERIAL
            } else androidId
        }

    /**
     * 隐藏软键盘
     */
    fun hideSoftInput(activity: Activity) {
        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        val isOpen = imm.isActive //isOpen若返回true，则表示输入法打开
        if (!isOpen || activity.currentFocus == null) {
            return
        }
        imm.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    fun restart() {
        try {
            var suPath = "su"
            if (File("/system/bin/su").exists()) {
                suPath = "/system/bin/su"
            } else if (File("/system/xbin/su").exists()) {
                suPath = "/system/xbin/su"
            }
            val proc = Runtime.getRuntime().exec(arrayOf(suPath, "-c", "reboot ")) //重启设备
            proc.waitFor()
        } catch (ex: Exception) {
            ex.printStackTrace()
            Log.e("spm", "reBoot e:$ex")
        }
    }
}